/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.editor.controller;

import java.awt.event.ActionEvent;
import java.net.URL;
import java.util.HashMap;
import javax.swing.text.Element;
import offset.nodes.client.editor.Editor;
import offset.nodes.client.editor.model.HyperlinkModel;
import offset.nodes.client.editor.model.Range;
import offset.nodes.client.editor.view.hyperlink.DataHyperlinkInfo;
import offset.nodes.client.editor.view.hyperlink.HyperlinkDialog;

/**
 *
 * @author Walter Lütgenau
 */
public class HyperlinkActions {

    public static abstract class AbstractHyperlinkAction extends EditorActions.AbstractEditorAction { 

        public AbstractHyperlinkAction(String name, Editor editor) {
            super(name, editor);
        }

        protected HyperlinkModel getModel() {
            return new HyperlinkModel(getDocumentContext(),editor);
        }        
    }

    public static class InsertHyperlinkAction extends AbstractHyperlinkAction {

        public InsertHyperlinkAction(Editor editor) {
            super(java.util.ResourceBundle.getBundle("offset/nodes/client/editor/view/resources/EditorBundle").getString("insert.hyperlink"), editor);
            setIcon(new javax.swing.ImageIcon(getClass().getResource("/offset/nodes/client/editor/view/resources/sc_hyperlink.png")));
        }
        
        @Override
        public void action(ActionEvent e) throws Exception {
            HyperlinkDialog dialog = new HyperlinkDialog(editor);
            if (dialog.showDialog() == HyperlinkDialog.CANCEL) {
                return;
            }

            Range range = new Range(editor.getBrowserPane().getSelectionStart(), editor.getBrowserPane().getSelectionEnd());
            
            Element element = getModel().getDocument().getParagraphElement(range.getStartOffset());
            
            int type = dialog.getHyperlinkType();
            switch (type) {
                case HyperlinkDialog.TYPE_INTERNAL:
                    getModel().insertInternalHyperlink(new Range(editor.getBrowserPane().getSelectionStart(),
                            editor.getBrowserPane().getSelectionEnd()), dialog.getInternalHyperlinkInfo());
                    break;
                    
               case HyperlinkDialog.TYPE_EXTERNAL:
                    getModel().insertExternalHyperlink(new Range(editor.getBrowserPane().getSelectionStart(),
                            editor.getBrowserPane().getSelectionEnd()), dialog.getExternalHyperlinkInfo());
                    break;
            }
            
            refreshCaret(editor, range);
        }
    }
}
